import { Notification } from 'element-ui'
import { send } from '@/store/websocket/socket'
import router from '@/router/routers'

class Invocation {
  constructor(type, message) {
    this.type = type
    this.message = message
  }
}

/**
 * 好友上线
 * @param user / 包含 username 头像
 */
export function online(user) {
  Notification.success({
    title: '好友上线通知',
    message: user.username + '上线',
    showClose: false,
    duration: 2000
  })
}

/**
 * 好友下线
 * @param user
 */
export function offline(user) {
  Notification.info({
    title: '好友下线通知',
    message: user.username + '上线',
    showClose: false,
    duration: 2000
  })
}

/**
 * 私聊 - 请求[发]
 * @param message 消息体
 */
export function sendMessageRes(message) {
  debugger
  const sendMsg = {
    type: 'SEND_SIMPLE_CHAT',
    message: message
  }
  send(JSON.stringify(sendMsg))
}

/**
 * 私聊 - 响应[收]
 * 单向绑定, 只有接受消息的用户才会绑定发送者的useId
 *
 * @param message
 * @param state
 */
export function receiveMessage(message, state) {
  debugger
  const chat = state.chatList.find(chat => chat.user.receiveId === message.sendId)
  if (chat) {
    chat.messages.push({
      content: message.content,
      date: new Date().getTime()
    })
    return
  }

  const result = state.friendList.find(friend => friend.userId === message.sendId)
  state.selectId = 1

  for (let i = 0; i < state.chatList.length; i++) {
    state.chatList[i].id++
    state.chatList[i].index++
  }
  // 因为是接受消息, sendId 字段可以不要, 则表示接受的消息不是自己本人发送的. 是不是本人发送, 只看 sendId 等不等于 用户userId
  state.chatList.unshift({
    id: 1,
    user: {
      username: result.username,
      img: result.img,
      // sendId: message.receiveId,
      receiveId: message.sendId
    },
    messages: [
      {
        content: message.content,
        date: new Date().getTime()
      }
    ],
    index: 1
  })
}

/**
 * 选择好友后，点击发送信息。判断在聊天列表中是否有该好友，有的话跳到该好友对话。没
 * 有的话添加该好友的对话 并置顶
 *
 * @param state
 */
export function sendMessage(state) {
  debugger
  const result = state.friendList.find(friend => friend.id === state.selectFriendIndex)
  const msg = state.chatList.find(msg => msg.user.username === result.remark)
  if (msg) {
    state.selectId = msg.index
    router.push({ path: '/home/chat' })
    return
  }

  state.selectId = 1
  for (let i = 0; i < state.chatList.length; i++) {
    state.chatList[i].id++
    state.chatList[i].index++
  }
  state.chatList.unshift({
    id: 1,
    user: {
      username: result.username,
      img: result.img,
      // sendId: state.user.userId
      receiveId: result.userId
    },
    messages: [
      {
        content: '已经置顶聊天，可以给我发信息啦！',
        date: new Date().getTime()
      }
    ],
    index: 1
  })
}

/**
 * 登录聊天室, 认证
 * @param message
 */
export function loginFuc(message) {
  const reqMsg = new Invocation('AUTHORIZATION', JSON.stringify(message))
  const msg = JSON.stringify(reqMsg)
  send(msg)
}

/**
 * 心跳
 *
 * @returns {string}
 */
export function heartbeat() {
  const heartbeat = {
    type: 'HEARTBEAT_REQUEST'
  }
  send(JSON.stringify(heartbeat))
}

/**
 * 保存聊天记录, 主动发送消息, 所以 sendId 是 当前用户userId
 *
 * @param state
 * @param msg
 */
export function pushMessage(state, msg) {
  const result = state.chatList.find(session => session.id === state.selectId)
  result.messages.push({
    content: msg.content,
    date: new Date().getTime(),
    self: true,
    sendId: state.user.userId
  })
}

export function pushGroupMessage(state, msg) {
  const result = state.chatList.find(session => session.id === state.selectId)
  result.messages.push({
    content: msg.content,
    date: new Date().getTime(),
    self: true,
    sendId: state.user.userId
  })
}

